import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem{
    public static void main() {
        Scanner sc=new Scanner(System.in);
        boolean flag=false;
        ArrayList<Student> list=new ArrayList<>();
        while (true){
            System.out.println("-----------------欢迎来到牛马学生管理系统-----------------");
            System.out.println("1: 添加学生");
            System.out.println("2: 删除学生");
            System.out.println("3: 修改学生");
            System.out.println("4: 查询学生");
            System.out.println("5: 退出");
            //用string是为了处理异常输入
            String choose=sc.next();
            //0写退出,1写添加,2写查询,3写删除,4写修改
            switch (choose){
                case "1"->addStudent(list);
                case "2"->delete(list);
                case "3"->modify(list);
                case "4"->query(list);
                case "5"->{
                    System.out.println("退出");
                    flag=true;
                }
                default -> System.out.println("无该选项,请重新输入");
            }
            if(flag){
                break;
            }
        }
    }
    public static void modify(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("修改学生");
        System.out.println("请输入需要修改的学生id:");
        String id=sc.next();
        int index=getindex(list,new Student(id,null,0,null));
        if(index==-1){
            System.out.println("不存在该学生的id,请添加后修改");
            return;
        }
        System.out.println("请输入学生的姓名:");
        String name=sc.next();
        System.out.println("请输入学生的年龄:");
        int age=sc.nextInt();
        System.out.println("请输入学生的地址:");
        String address=sc.next();
        list.add(new Student(id,name,age,address));
        System.out.println("修改完成");
    }
    public static void delete(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("删除学生");
        System.out.println("请输入要删除学生的id:");
        String id=sc.next();
        int index=getindex(list,new Student(id,null,0,null));
        if(index==-1){
            System.out.println("该学生不存在");
            return;
        }
        list.remove(index);
        System.out.println("删除成功");
    }
    public static void addStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("添加学生");
        String id,name,address;
        int age;
        while (true){
            System.out.println("请输入学生的id:");
            id=sc.next();
            //封装思想
            //由于contains方法与求index方法大体相同可以调用
            boolean flag=contains(list,new Student(id,null,0,null));
            if(flag){
                //包含
                System.out.println("该学生的id已存在,请重新输入");
            }else{
                break;
            }
        }
        System.out.println("请输入学生的姓名");
        name=sc.next();
        System.out.println("请输入学生的年龄");
        age=sc.nextInt();
        System.out.println("请输入学生的家庭住址");
        address=sc.next();
        list.add(new Student(id,name,age,address));
        System.out.println("添加学生成功");
    }
    public static void query(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("查询学生");
        System.out.println("1:查询全部学生信息");
        System.out.println("2:查询指定ID的学生信息");
        System.out.println("请输入您的选择");
        String choose=sc.next();
        //由于存在非法输入,故有查询失败的情况,所以表头信息将在方法里展示
        switch (choose){
            case "1"-> queryAll(list);
            case "2"-> queryId(list);
            default -> System.out.println("查询失败");
        }
        System.out.println("查询完毕");
    }
    public static void queryAll(ArrayList<Student> list){
        //打印表头信息
        System.out.println("id\t\tname\t\tage\t\tadress\t\t");
        int size=list.size();
        for (int i = 0; i < size; i++) {
            Student stu=list.get(i);
            //由于要多次调用get方法获取,所以将student类写上方法show
            //System.out.println();
            stu.show();
        }
    }
    public static void queryId(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要查询学生的id:");
        String id=sc.next();
        int index=getindex(list,new Student(id,null,0,null));
        if(index==-1){
            //不存在,输出提示
            System.out.println("不存在该id的学生,请添加后再进行查询");
            return;
        }
        //运行到这里说明找到
        //打印表头信息
        System.out.println("id\t\tname\t\tage\t\tadress\t\t");
        //打印学生信息
        list.get(index).show();
    }
    public static boolean contains(ArrayList<Student> list,Student stu){
        if(getindex(list,stu)==-1){
            return false;
        }
        return true;
    }
    public static int getindex(ArrayList<Student> list,Student stu){
        int size=list.size();//避免多次调用,加快效率
        String id=stu.getId();
        for (int i = 0; i < size; i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
